package com.seteam.collabcrossword.workers;

import com.aonaware.services.webservices.Definition;
import com.aonaware.services.webservices.DictServiceSoapProxy;
import com.aonaware.services.webservices.WordDefinition;
import com.seteam.collabcrossword.CollabCrosswordWorker;
import com.seteam.collabcrossword.messages.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;

public class DefinitionFetcher extends SwingWorker<String, String> implements CollabCrosswordWorker{
    private final Logger logger = LoggerFactory.getLogger(DefinitionFetcher.class);
    private final String word;
    private DictServiceSoapProxy soapProxy;
    private Definition[] meanings;
    private static final String DICTIONARY = "wn";
    private long _id;

    public DefinitionFetcher(final String word) {
        this.word = word;
        soapProxy = new DictServiceSoapProxy();
        _id = Thread.currentThread().getId();
    }


    @Override
    protected String doInBackground() throws Exception {
        final WordDefinition wordDefinition = soapProxy.defineInDict(DICTIONARY, word);
        meanings = wordDefinition.getDefinitions();
        if(meanings.length == 0) {
            return "No definition found";
        }
        logger.info("Thread ID: {}", Thread.currentThread().getId());
        return meanings[0].getWordDefinition();
    }

    public long getID() {
        return _id;
    }

    @Override
    public void receive(final Message message) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
